/* ***************************
* 电子表格预览界面分页JS
* @Editer panzining
* ****************************/

/**
 * 电子表格预览分页查询
 * @param type
 * @param sheetid
 * @returns {*}
 */
function previewPageQuery(type, sheetid) {
    var lastPage = Number($("#lastpage_" + sheetid).val());
    var cpage = Number($("#currentpage_" + sheetid).val());

    var tpage = 1;  //目标页
    switch (type) {
        case "f":   //第一页
            tpage = 1;
            break;
        case "p":   //上一页
            tpage = cpage - 1;
            if (tpage <= 0) {
                return showInfoAlert("没有上一页了！");
            }
            break;
        case "n":   //下一页
            tpage = cpage + 1;
            if (tpage > lastPage) {
                return showInfoAlert("没有下一页了！！!");
            }
            break;
        case "l":   //最后一页
            tpage = lastPage;
            break;
        case "j":   //跳转
            tpage = Number($("#jumpinput_" + sheetid).val());
            if (tpage > lastPage || tpage <= 0) {
                return showInfoAlert("错误的页数范围！！!页码范围[1~" + lastPage + "].");
            }
            break;
    }
    $("#currentpage_" + sheetid).val(tpage);

    previewExecQuery(sheetid);
}

/**
 * 电子表格执行分页查询
 * @param sheetid
 */
function previewExecQuery(sheetid) {
    showmodalmethod(null);  //显示遮罩层

    var currpage = Number($("#currentpage_" + sheetid).val());
    var pagesize = Number($("#currentsize_" + sheetid).val());
    var limitsize = Number($("#limitSize" + sheetid).val());

    if (currpage * pagesize > limitsize) {
        showInfoAlert("最多可展示前" + limitsize + "条数据！", null);
        currpage = limitsize / pagesize;
        $("#currentpage_" + sheetid).val(currpage);
    }

    var url = "/analysis/composesheet/preview";
    if ($("input[name='sheetType']").val() == 1) {
        url = "/analysis/spreadsheet/preview";
    }

    ajax({
        url: url,
        data: {
            sheetid: sheetid,
            page: currpage,
            pageSize: pagesize,
            limitSize: limitsize,
            daterange: $("#daterange").val(),
            datepicker: $("#datepicker").val(),
            textParamJson: $("#textParamJson").val(),
            selectParamJson: $("#selectParamJson").val(),
            timeParamJson: $("#timeParamJson").val(),
            fuzzyParamJson: $("#fuzzyParamJson").val()
        },
        error: function (request) {
            canclemodalmethod();    //取消遮罩层
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (result) {
            canclemodalmethod();    //取消遮罩层

            if (result.returnCode === "1") {
                //清空上次查询结果
                var datahead = $("#table_head_" + sheetid);
                var databody = $("#table_body_" + sheetid);
                datahead.empty();
                databody.empty();

                var page = result.page;
                //分页信息
                var end = page.start + page.pageSize;
                if (end > page.totalItems) {
                    end = page.totalItems;
                }

                $("#pagination-info_" + sheetid).text("显示 " + page.start + " - " + end + " 共 " + page.totalItems + " 条记录");
                $("#lastpage_" + sheetid).val(page.totalPages);
                var lastPage = page.totalPages;

                if (page.pn === 1) {
                    //当前页码为1时  第一页和上一页不可用
                    $("#queryFBtn_" + sheetid).addClass("disabled");
                    $("#queryPBtn_" + sheetid).addClass("disabled");
                } else {
                    $("#queryFBtn_" + sheetid).removeClass("disabled");
                    $("#queryPBtn_" + sheetid).removeClass("disabled");
                }

                if (page.pn === lastPage) {
                    //当前页码为最后一页时  最后一页和下一页不可用
                    $("#queryNBtn_" + sheetid).addClass("disabled");
                    $("#queryLBtn_" + sheetid).addClass("disabled");
                } else {
                    $("#queryNBtn_" + sheetid).removeClass("disabled");
                    $("#queryLBtn_" + sheetid).removeClass("disabled");
                }

                var rows = page.items;
                //数据信息
                var startItem = 0;
                var lastItem = rows.length;

                //若多于50条则只显示50条
                if (rows.length > pagesize) {
                    startItem = (currpage - 1) * pagesize;
                    lastItem = startItem + pagesize;
                }
                if (rows.length > 0) {
                    for (var i = startItem; i < lastItem; i++) {
                        var row = rows[i];
                        if (i === startItem) {
                            //表头
                            datahead.append('<th style="min-width: 50px;width: 50px"></th>');
                            for (dthi in row.heads) {
                                var dth = row.heads[dthi];
                                datahead.append("<th nowrap='nowrap'>" + dth + "</th>");
                            }
                        }
                        //数据
                        var datatr = $("<tr>");
                        for (dtdi in row.values) {
                            var dtd = row.values[dtdi];
                            datatr.append("<td nowrap='nowrap'>" + dtd + "</td>");
                        }
                        databody.append(datatr);
                    }
                } else {
                    showInfoAlert("没有查询到记录！", null);
                }
            }
        }
    });
}